import 'dart:io';
import 'dart:ui';


import 'package:example/common/routes/app_pages.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';

import 'common/config/config.dart';
import 'common/langs/translation_library.dart';
import 'common/styles/theme.dart';
import 'pages/main/main_desktop_view.dart';
import 'pages/main/main_view.dart';

/// Main
void main() async {
  await ScreenUtil.ensureScreenSize();
  await init(
    isDebug: kDebugMode,
    logTag: 'GetxScaffoldOhos',
    supportedLocales: TranslationLibrary.supportedLocales,
  );
  /// 网络配置初始化
  await HttpConfig.init();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    final aspectRatio = MediaQuery.of(context).size.aspectRatio;
    if (kDebugMode) {
      print("aspectRatio: $aspectRatio");
    }
    refreshAppui();
  }

  @override
  Widget build(BuildContext context) {

    return LayoutBuilder(
      builder: (context, cts){
        final Size designSize = _selectDesignSize(context);

        if (kDebugMode){
          print("'物理尺寸：${View.of(context).physicalSize}'");
          print("designSize: $designSize");
        }


        return RefreshConfiguration(
          headerBuilder: () => const ClassicHeader(), // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
          footerBuilder: () => const ClassicFooter(), // 配置默认底部指示器
          springDescription:const SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
          enableLoadingWhenFailed : true, //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
          hideFooterWhenNotFull: false, // Viewport不满一屏时,禁用上拉加载更多功能
          enableBallisticLoad: true, // 可以通过惯性滑动触发加载更多
          child: GetxApp(
            // 设计稿尺寸 单位：dp
            designSize: designSize,
            minTextAdapt: true,
            splitScreenMode: true,
            // Getx Log
            enableLog: kDebugMode,
            // 默认的跳转动画
            defaultTransition: Transition.rightToLeft,
            // 主题模式
            themeMode: GlobalService.to.themeMode,
            // 主题
            theme: AppTheme.light,
            // Dark主题
            darkTheme: AppTheme.dark,
            // 国际化配置
            locale: GlobalService.to.locale,
            translations: TranslationLibrary(),
            fallbackLocale: TranslationLibrary.fallbackLocale,
            supportedLocales: TranslationLibrary.supportedLocales,
            localizationsDelegates: TranslationLibrary.localizationsDelegates,
            // AppTitle
            title: 'GetxScaffoldOhos',
            // 首页
            // home: PlatformUtil.isDesktopUI ? MainDesktopView() : MainPage(),
            initialRoute: AppPages.INITIAL,
            getPages: AppPages.routes,
            // Builder
            builder: (context, widget) {
              // do something....
              return widget!;
            },
          ),
        );
      },
    );
  }

  /// 选择设计稿尺寸
  ///
  Size _selectDesignSize(BuildContext  context) {

    final size = MediaQuery.of(context).size;
    final orientation = MediaQuery.of(context).orientation;

    final screenWidth = orientation == Orientation.portrait ? size.width : size.height;

    if (kDebugMode) {
      print("operatingSystem: ${Platform.operatingSystem}");
      print("size: $size");
      print("orientation: $orientation");
      print("screenWidth: $screenWidth");
    }
    if(PlatformUtil.isDesktopUI) return const Size(1440, 900); // 桌面端
    if (screenWidth > 600) return orientation == Orientation.portrait ? const Size(768, 1024): const Size(1024, 768);// 平板通用
    return orientation == Orientation.portrait? const Size(375, 812): const Size(812, 375);// 手机
  }
}
